﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace Mobile
{
    public partial class login : System.Web.UI.Page
    {
        private Dx.ZcEncrypt.Encrypt3DES encrypt = new Dx.ZcEncrypt.Encrypt3DES();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Cookies["_mobile_autologin"] != null)
            {
                if (!string.IsNullOrEmpty(Request.Cookies["_mobile_autologin"].Values["loginid"])
                    ||
                    !string.IsNullOrEmpty(Request.Cookies["_mobile_autologin"].Values["ExpiresDate"]))//不为空
                {
                    DateTime _n = Convert.ToDateTime(Request.Cookies["_mobile_autologin"].Values["ExpiresDate"]);
                    if (DateTime.Now.CompareTo(_n) < 0)//不过期
                    {
                        string _loginid = encrypt.DecryptString(Request.Cookies["_mobile_autologin"].Values["loginid"]);
                        string _pwd = encrypt.DecryptString(Request.Cookies["_mobile_autologin"].Values["pwd"]);
                        this.txt_name.Value = _loginid;
                        //this.txtPsd.Attributes.Add("value", _pwd);
                        CheckLogin(_pwd);
                        return;
                    }
                }
            }

            if (Request.Cookies["_mobile_recordaccount"] != null)
            {
                if (!string.IsNullOrEmpty(Request.Cookies["_mobile_recordaccount"].Values["loginid"]) ||
                !string.IsNullOrEmpty(Request.Cookies["_mobile_recordaccount"].Values["ExpiresDate"]))//不为空
                {
                    DateTime _n2 = Convert.ToDateTime(Request.Cookies["_mobile_recordaccount"].Values["ExpiresDate"]);
                    if (DateTime.Now.CompareTo(_n2) < 0)//不过期
                    {
                        string _loginid2 = encrypt.DecryptString(Request.Cookies["_mobile_recordaccount"].Values["loginid"]);

                        string _pwd2 = encrypt.DecryptString(Request.Cookies["_mobile_recordaccount"].Values["pwd"]);

                        this.txt_name.Value = _loginid2;
                        //this.txtPsd.Attributes.Add("value", _pwd2);
                        this.cb1.Checked = true;
                        return;
                    }
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            CheckLogin("");
        }

        private void CheckLogin(string psd)
        {
            string loginId = this.txt_name.Value.Trim();//txt_name.Value.ToString();

            string password = this.txtPsd.Text.Trim();//txtPsd.Text.ToString();
            if (!string.IsNullOrEmpty(psd))
            {
                password = psd;
            }

            Hashtable param = new Hashtable();
            param["username"] = loginId;
            //param["userpwd"] = txb_userpwd.Text;
            Hashtable user = ERMap.ERMapHelper.getmodel("sys_users", param);
            #region 登录日志记录
            Hashtable logininfo = new Hashtable();
            logininfo["loginlogid"] = "0";
            logininfo["loginid"] = loginId;
            logininfo["usertype"] = "4";
            logininfo["userid"] = "0";
            logininfo["success"] = "登录成功";
            logininfo["localip"] = Commons.NetSocket.getAllIp();
            logininfo["logintime"] = DateTime.Now;
            //model["OutSysTime"] = "0";
            #endregion
            //if (!txb_check.Text.Equals(Session["web_login_checkcode"]))
            //{
            //    ltrShowMessage.Text = "验证码错误";
            //    logininfo["success"] = "登录失败";
            //    logininfo["reason"] = lt_msg.Text;
            //    div_notify.Visible = true;
            //    PhoneBook.Manage.LoginLog.add(logininfo);
            //    return;
            //}

            if (user == null)
            {
                ltrShowMessage.Text = "用户不存在";
                logininfo["success"] = "登录失败";
                logininfo["reason"] = ltrShowMessage.Text;
                PhoneBook.Manage.LoginLog.add(logininfo);
                return;
            }
            else
            {
                logininfo["usertype"] = user["usertype"];
                logininfo["userid"] = user["userid"];
            }
            
            //El6WuA4jUzKLbI7aQicoCkHD7B7PAxW14sni/iaQ9NMC2rS1WD1m8g==  初始密码123456
            if (!encrypt.ValidPwdBy3des(password, Commons.StrUtil.FormatValue(user["userpwd"])))
            {
                ltrShowMessage.Text = "密码错误";
                logininfo["success"] = "登录失败";
                logininfo["reason"] = ltrShowMessage.Text;
                PhoneBook.Manage.LoginLog.add(logininfo);
                return;
            }

            if (logininfo["success"].ToString() == "登录成功")
            {
                Session["userid"] = user["userid"];
                Session["username"] = user["username"];
                Session["nickname"] = user["nickname"];
                Session["usertype"] = user["usertype"];
                #region
                logininfo["reason"] = "登录成功";
                PhoneBook.Manage.LoginLog.add(logininfo);
                #endregion
                Response.Redirect("~/search.aspx");

                #region 记录cookie
                if (this.cb1.Checked)//记住帐号
                {
                    SettingCookie(loginId, "_mobile_recordaccount");
                }

                if (this.cb2.Checked)//自动登录
                {
                    SettingCookie(loginId, "_mobile_recordaccount");
                    SettingCookie(loginId, "_mobile_autologin");
                }
                #endregion
            }
        }

        private void SettingCookie(string LoginId, string cookiename)
        {
            string _EncryptLoginid = encrypt.EncryptString(LoginId);
            string _EncryptPwd = encrypt.EncryptString(txtPsd.Text.Trim());
            HttpCookie _mobile_recordaccount = System.Web.HttpContext.Current.Request.Cookies[cookiename];

            if (_mobile_recordaccount != null)
            {
                _mobile_recordaccount.Values.Set("loginid", _EncryptLoginid);
                _mobile_recordaccount.Values.Set("pwd", _EncryptPwd);
                _mobile_recordaccount.Values.Set("ExpiresDate", DateTime.Now.AddDays(7).ToString());
                _mobile_recordaccount.Expires = DateTime.Now.AddDays(7);
                System.Web.HttpContext.Current.Response.SetCookie(_mobile_recordaccount);
            }
            else
            {
                _mobile_recordaccount = new HttpCookie(cookiename);
                _mobile_recordaccount.Values.Add("loginid", _EncryptLoginid);
                _mobile_recordaccount.Values.Add("pwd", _EncryptPwd);
                _mobile_recordaccount.Values.Add("ExpiresDate", DateTime.Now.AddDays(7).ToString());
                _mobile_recordaccount.Expires = DateTime.Now.AddDays(7);
                System.Web.HttpContext.Current.Response.AppendCookie(_mobile_recordaccount);
            }
        }
    }
}